import java.sql.SQLOutput;
import java.util.Arrays;

public class Task {
    //数组：课堂代码练习与数组作业

    public static void main(String[] args) {
        int[] arr1 = new int[]{1,2,3,4,5};
        int[] arr2 = new int[]{1,2,3,4,5};
        boolean bool = Arrays.equals(arr1,arr2);
        System.out.println(bool);
    }
    public static void main24(String[] args) {
        String arr1 = "abcdef";
        String arr2 = "abcd";
        String arr3 = "abcdef";
        boolean bool = arr1.equals(arr2);
        System.out.println(bool);

        bool = arr3.equals(arr1);
        System.out.println(bool);
    }


    public static void main22(String[] args) {
        int[][] arr = new int[3][];
        arr[0] = new int[3];//第一行有三列
        arr[1] = new int[2];//第二行有两列
        arr[2] = new int[4];//第三行有四列
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }
    public static void main21(String[] args) {
        int[][] arr1 = new int[][]{{1,2,3},{4,5,6}};
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[0].length; j++) {
                System.out.print(arr1[i][j]+" ");
            }
            System.out.println();
        }
      /* for (int[] X:arr1) {
           for (int Y:X) {
               System.out.print(Y+" ");
           }
           System.out.println();
       }*/
       /* String ret = Arrays.deepToString(arr1);
        System.out.println(ret);
*/
    }

    public static void main13(String[] args) {
        //冒泡排序
        int[] arr = {10,9,8,7,6,5,4,3,2,1};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));

    }
    public static void bubbleSort(int[] arr) {
        //升序
        int i =0;
        for (i = 0; i < arr.length-1; i++) {

            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j]>arr[j+1]) {
                  int tmp = arr[j+1];
                  arr[j+1] = arr[j];
                  arr[j] = tmp;

                }
            }//两两比较之后，都没发生交换说明有序了


        }
    }

    public static void main23(String[] args) {
        //介绍一些其他的数组方法
        //1.Arrays.fill，数组局部填充
        int[] arr = new int[10];
        Arrays.fill(arr,1,5,11);//填充下标[1,5)
        System.out.println(Arrays.toString(arr));

    }
    public static void main11(String[] args) {
        //逆序数组
        int[] arr = {10,9,8,7,6,5,4,3,2,1,0};
        reverse(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void reverse(int[] arr) {
        int left = 0;
        int right =arr.length-1;
        while(left < right) {//相等的时候就不需要逆序了
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }

    }
    public static void main10(String[] args) {
        //1.折半查找，要求数组内容为有序.找到了返回下标
        int[] arr1 = {2,5,7,8,10,11,15,17,20,22};
        int ret = binarySearch(arr1,10);
        System.out.println(ret);
        //2.当数组无序时，使用Array.sort排序后折半查找
        int[] arr2 = {9,8,7,6,5,4,3,2,1,0};
        Arrays.sort(arr2);
        System.out.println(Arrays.toString(arr2));
        int cur = binarySearch(arr2,11);
        System.out.println(cur);
    }
    public static int binarySearch(int[] arr,int k) {
        int left = 0;
        int right = arr.length-1;
        while(left <= right) {
            //从中间位置开始找
            int mid = (left+right)/2;
            if(k < arr[mid]) {//k在左边
                right=mid-1;
            } else if(k > arr[mid]) {
                //在右边
                left=mid+1;
            } else {
                return mid;
            }
        }
        return -1;
    }
    public static void main9(String[] args) {
        //在数组中查找某个值，找到返回下标，找不到返回-1
        int[] arr = {5,4,3,2,1};
        int k = 0;
        int ret = find(arr,k);
        System.out.println(ret);
    }
    public static int find(int[] arr,int k) {
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==k) {
                return i;
            }
        }
        return -1;
    }
    public static void main20(String[] args) {
        //数组拷贝方法1
        //1.常规拷贝法引入
        int[] arr = new int[]{1,2,3,4,5};
        int[] copy = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            copy[i] = arr[i];
        }
        System.out.println(Arrays.toString(copy));
        //2.使用Array中的copyOf方法拷贝
        copy = Arrays.copyOf(arr,arr.length-2);//被拷贝的数组名和长度
        System.out.println(Arrays.toString(copy));
        //3.拷贝数组，数组是原数组的两倍空间（指向了一块新空间）
        arr = Arrays.copyOf(arr,arr.length*2);
        arr[5]=6;
        arr[6]=7;
        arr[7]=8;
        arr[8]=9;
        arr[9]=10;
        System.out.println(Arrays.toString(arr));
        //4.使用System.arraycopyOf将指定源数组中的数组从指定位置复制到目标数组的指定位置。
        //arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
        int[] dest = new int[arr.length];
        System.arraycopy(arr,2,dest,0,arr.length-3);//[2,arr.length-3)55551
        System.out.println(Arrays.toString(dest));
        //5.数组名.clone()
        int[] copy1 = arr.clone();
        System.out.println(Arrays.toString(copy1));
        //6.非拷贝写法
        int[] copy2 = arr;
        System.out.println(Arrays.toString(copy2));


    }
    public static void main7(String[] args) {
        //模拟实现toString()--将数组 转变为字符串进行返回
        int[] arr = new int[]{11,22,33,44,55,66,77};
        String str = myToString(arr);
        System.out.println(str);
    }
    public static String myToString(int[] arr) {
        if(arr==null) {//判空操作
            return null;
        }
        String ret = "[";
        for (int i = 0; i < arr.length; i++) {
            ret+=arr[i];
            if(i!=arr.length-1) {
               ret+=",";
            }
        }
        ret+="]";
       return ret;
    }
    public static void main6(String[] args) {
        //传参完成两个数字的交换（只能是引用类型）
        int[] arr = {520,1314};
        System.out.println("交换前：a="+arr[0]+",b="+arr[1]);
        swap(arr);
        System.out.println("交换后：a="+arr[0]+",b="+arr[1]);

    }
    public static void swap(int[] arr) {
        int tmp = arr[0];
        arr[0] = arr[1];
        arr[1] = tmp;
    }
    public static void main5(String[] args) {
        //基本数据类型与引用类型的区别（传参）

        //1.基本数组类型传参之后不能被修改（不能获得其地址）
        int a = 10;
        func1(a);
        System.out.println(a);
        //2.不能修改的引用类型传参
        int[] arr1 = {1,2,3,4,5};
        func2(arr1);
        System.out.println(Arrays.toString(arr1));
        //3.传引用不能修改值得另一个写法（返回值）
        int[] ret = func3();
        System.out.println(Arrays.toString(arr1));
        //4.传引用可以修改的方式
        func4(arr1);
        System.out.println(Arrays.toString(arr1));

    }
    public static void func1(int a) {
        a = 20;
    }
    public static void func2(int[] arr1) {
        //2.不能修改的传引用类型(旧的引用指向了新的地址)
        arr1 = new int[]{11,22,33,44,55};
    }
    public static int[] func3() {
        //(旧的引用指向了新的地址)
        int[] array = new int[]{11,22,33,44,55};
        return array;
    }
    public static void func4(int[] arr) {
        arr[0] = 11;
        arr[1] = 22;
        arr[2] = 33;
        arr[3] = 44;
        //或者其他的修改方式
    }

    public static void main4(String[] args) {
        //数组传参1
        int[] arr = {1,3,1,4};
        print(arr);
    }
    public static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }

    public static void main3(String[] args) {
        //下面的代码看录播复习
        //int[] array = null;
        //System.out.println("数组长度："+array.length);//会报空指针异常
        //System.out.println(array[0]);
        //array[0] = 99;
        //System.out.println(array[0]);

        //数组的几种遍历方式
        //1.for循环遍历
        int[] arr1 = {5,2,0,1,3,1,4};
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i]+" ");
        }
        System.out.println();
        //2.使用for-each（加强for语句），一般只能用来遍历
        for (int Y:arr1) {//int Y的意思是用数组元素的类型去定义一个遍历Y，Y中存放数组的内容
            System.out.print(Y+" ");
        }
        int[] arr2 = {9,8,7};
        System.out.println();
        //3.使用Arrays(类)中的toString方法
        System.out.println(Arrays.toString(arr2));//直接用返回值做print的参数

        String ret = Arrays.toString(arr2);
        System.out.println(ret);

    }
    public static void main2(String[] args) {
        //创建数组的几种方式
        //1.直接new一块空间给数组arr，大小为10个元素,每个数据为0
        int[] arr1 = new int[10];
        //2.C语言中常用的，初始化时就赋值
        int[] arr2 = {1,2,3,4,5};
        //3.new空间并且赋值
        int[] arr3 = new int[]{1,2,3,4,5};
        //4.错误写法
        //int[] arr4;
        //arr4 = {1,2,3};

    }
    public static void main1(String[] args) {
        //创建的数组，并且赋初始值
        int[] arr = new int[100];
        for (int i = 0; i < arr.length; i++) {
            arr[i]=i+1;
        }
        System.out.print(Arrays.toString(arr));
    }
}
